package com.example.treehole.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.treehole.bean.Callback;
import com.example.treehole.common.BaseResult;
import com.example.treehole.service.CallbackServiceImpl;
import com.example.treehole.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/callback")
public class CallbackController {
    @Autowired
    private CallbackServiceImpl callbackService;
    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/get")
    public ResponseEntity<?> get(String userid) {
        LambdaQueryWrapper<Callback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!userid.equals("admin"), Callback::getUserid, userid);
        List<Callback> list = callbackService.list(queryWrapper);
        for (Callback callback : list) {
            callback.setUser(userService.getById(callback.userid));
        }
        return BaseResult.ok("获取成功", list);
    }
 

    @PostMapping("/add")
    public ResponseEntity<?> add(@RequestBody Callback callback) {
        callbackService.save(callback);
        return BaseResult.ok("提交成功");
    }

     @PostMapping("/update")
     public ResponseEntity<?> update(@RequestBody Callback callback) {
         callbackService.updateById(callback);
         return BaseResult.ok("更新成功！");
     }

     @DeleteMapping("/delete")
     public ResponseEntity<?> delete(String id) {
         callbackService.removeById(id);
         return BaseResult.ok("删除成功！");
     }

}
